package com.int_tech.newTimeTask;

import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/29.
 */
@Component
public class CheckMessionIsOverTimeTask {

    @Autowired
    private MessionDao messionDao;

    /**
     * 验证任务单是否结束
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkMessionIsOver() throws ParseException {

        List<Mession> messionList = messionDao.findByIsOver(0);

        for (Mession mession :messionList)
        {
            try
            {
                String startAddress = mession.getStartAddress();
                String endAddress = mession.getEndAddress();
                Date startOpenDoorTime = mession.getStartOpenDoorTime();
                Date startCloseDoorTime = mession.getStartCloseDoorTime();
                Date endOpenDoorTime = mession.getEndOpenDoorTime();
                Date endCloseDoorTime = mession.getEndCloseDoorTime();

                if(StringUtils.isNotEmpty(startAddress) && StringUtils.isNotEmpty(endAddress) && startOpenDoorTime != null && startCloseDoorTime != null &&endOpenDoorTime != null && endCloseDoorTime != null)
                {
                    mession.setIsOver(1);
                    messionDao.save(mession);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
